package com.hskn.hss.module.equiptype.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.empPlan.mapper.EmpPlanMapper;
import com.hskn.hss.module.employeeCorrectRecord.entity.EmployeeCorrectRecord;
import com.hskn.hss.module.equiptype.entity.TlEquiptype;
import com.hskn.hss.module.equiptype.mapper.TlEquiptypeMapper;
import com.hskn.hss.module.equiptype.service.ITlEquiptypeService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Slf4j
@RestController
@RequestMapping("/hss/tlequiptype")
public class TlEquiptypeController {

    @Autowired
    ITlEquiptypeService tlEquiptypeService;
    @Resource
    TlEquiptypeMapper tlEquiptypeMapper;

    @SysLog(businessType = BusinessType.INSERT, tableName = "tl_equiptype", bean = TlEquiptype.class, beanMapper = TlEquiptypeMapper.class, names = "equipname")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(@RequestBody @Validated TlEquiptype tlEquiptype, HttpServletRequest request) {
        try {
            QueryWrapper<TlEquiptype> tlEquiptypeQueryWrapper = new QueryWrapper<>();
            tlEquiptypeQueryWrapper.lambda().eq(TlEquiptype::getEquipname, tlEquiptype.getEquipname());
            List<TlEquiptype> tlEquiptypeList = tlEquiptypeMapper.selectList(tlEquiptypeQueryWrapper);
            if (tlEquiptypeList.isEmpty()) {
                tlEquiptypeService.insert(tlEquiptype);
                return AjaxResult.success(tlEquiptype);
            } else {
                return AjaxResult.fail("该名称已存在，请重新输入");
            }
        }  catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "tl_equiptype", idKey = "id", bean = TlEquiptype.class, beanMapper = TlEquiptypeMapper.class, names = "equipname")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return tlEquiptypeService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "tl_equiptype", idKey = "id", bean = TlEquiptype.class, beanMapper = TlEquiptypeMapper.class, names = "equipname")
    @RequestMapping(value = "deletebyid", method = RequestMethod.POST)
    public AjaxResult<Object> deletebyid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return tlEquiptypeService.deletebyid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getSelectList", method = RequestMethod.POST)
    public AjaxResult<Object> getSelectList() {
        return AjaxResult.success(tlEquiptypeService.getSelectList());
    }
}

